/*
  this file contains functions related to obtaining and displaying
  file and folder data, most of these functions are assigned to much
  easier to use objects in the Layouts file
*/

// get data for a folder id
function getFolder(id) {

  if (huddle.isAuth()) { // check authentication first
    
    json.getData("folder", folder.display, id);
  
  } else {
  
    page.switchContent("login",null);
  
  }

}

// display folder data
function displayFolder(data) {

  items = data.Data;
  
  //drag.set("huddle", "in"); // removed to prevent errors in drag and drop functionality
  
  if (items.length == 0) {
  
    $("#huddle>div>div").append("<p id='breadcrumbs'></p><span>This folder is empty</span>");
    
    breadcrumbs.write();
  
  } else {
  
    makeTable(items); // make a table with all the obtained items
    
  }

}

// get data for a file id
function getFile(id) {

  if (huddle.isAuth()) { // check authentication first
    
    json.getData("file", displayFile, id);
  
  } else {
  
    page.switchContent("login",null);
  
  }

}

// display data for a file
function displayFile(data) {

  file = data.Data;

  icon = getIcon(file.FileName); // get the icon

  output = "<span id='file'><img src='imgs/icons/" + icon + "-50.png' class='elementimg'>";
  
  output = output + "<p><span>Title:</span> " + file.Title + "</p>";
  output = output + "<p><span>File name:</span> " + file.FileName + "</p>";
  
  if (file.Description) { // check description exists before writing
  
    output = output + "<p><span>Description:</span> " + file.Description + "</p>";
    
  }
  
  output = output + "<p><span>Created:</span> " + file.Created.Day + "/" + file.Created.Month + "/" + file.Created.Year + " by <i>" + file.CreatedByUserDisplayName + "</i></p>";
  output = output + "<p><span>Last edited:</span> " + file.LastUpdated.Day + "/" + file.LastUpdated.Month + "/" + file.LastUpdated.Year + " by <i>" + file.LastEditedByUserDisplayName + "</i></p>";
  output = output + "<p><span>File size:</span> " + file.Size + " bytes</p>";
  output = output + "<p><span>Version:</span> " + file.Version + "</p>";
  
  output = output + "<p id='downloadLink'>Download this file</p></span>";
  
  $("#huddle>div>div").append("<p id='breadcrumbs'></p>" + output); // append with breadcrumbs holder
  
  breadcrumbs.write(); // write breadcrumbs in
  
  window.currentFileId = "File:" + file.Id; // set current file for download link

  // listener for click to download link
  document.getElementById("downloadLink").addEventListener("click", function() { download.startDownload(window.currentFileId); } );

}